//#region 引入模块
const path = require("path");
const express = require("express");
const bodyParser = require("body-parser");
const db = require("./db/db.js");
const cors = require("cors");
//#endregion

let app = express();
// ajax响应不显示服务端使用框架
app.disable("x-powered-by");

//#region
// 配置静态目录
app.use(express.static(path.join(__dirname, "public")));
// 解析post请求参数
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

// 配置跨域白名单, 允许携带cookie
app.use(
  cors({
    origin: [
      "http://127.0.0.1:8080",
      "http://localhost:8080",
      "http://127.0.0.1:5500",
      "http://m.zfzsjz.com",
      "https://www.zfzsjz.com",
      "http://test.xyfight.com:3001",
    ],
    credentials: true,
  })
);

// app.all('*', function(req, res, next) {
//   res.setHeader("Access-Control-Allow-Origin", "http://localhost:8080");
//   // res.setHeader("Access-Control-Allow-Credentials", true);
//   // res.header("Access-Control-Allow-Headers", "X-Requested-With");
//   res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
//   res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
//   res.header("X-Powered-By",' 3.2.1');
//   res.header("X-Frame-Options", 'deny')
//   res.header("Content-Type", "application/json;charset=utf-8");
//   next();
// });
//#endregion

//#region 配置路由
const testR = require("./routers/test");
app.use("/test", testR);

//#endregion
db.query("select * from test")
  .then((res) => {
    console.log(res);
  })
  .catch((err) => {
    console.log(err);
  });

app.listen(3001, function () {
  console.log(
    "\x1B[36m%s\x1B[0m",
    "------监听端口 http://127.0.0.1:3001 ------"
  );
});
//#region
//#endregion
